﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CashFlow.Db.Entity;
using CashFlow.Db.DAL;

namespace SysTool._02_Service
{
    public class ShopService
    {
        public static ShopInfoEntity QueryShopInfoByReferenceInvitationCode(string invitationCode)
        {
            ShopInfoEntity si = null;

            string tmpInvitationCode = invitationCode;
            using (var db = SqlSugarHelper.GetDb())
            {
                while (true)
                {
                    var us = db.Queryable<UserInfoEntity>()
                        .LeftJoin<ShopInfoEntity>((u, s) => u.Id == s.ShopManagerUserId)
                        .Where((u, s) => u.InvitationCode == tmpInvitationCode)
                        .Select((u, s) => new { UserInfo = u, ShopInfo = s })
                        .First();
                    if (us != null)
                    {
                        if (us.ShopInfo != null && us.ShopInfo.Id > 0)
                        {
                            si = us.ShopInfo;
                            break;
                        }

                        if (us.UserInfo != null)
                        {
                            tmpInvitationCode = us.UserInfo.ReferenceInvitationCode;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return si;
        }

        public static ShopInfoEntity QueryShopInfoByUserId(int userId)
        {
            ShopInfoEntity si = null;

            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>().Where(x => x.ShopManagerUserId == userId).First();
                if (entity != null)
                {
                    return entity;
                }
                else
                {
                    var ui = db.Queryable<UserInfoEntity>().Where(x => x.Id == userId).First();
                    if (ui != null)
                    {
                        string invitationCode = ui.InvitationCode;
                        while (true)
                        {
                            var us = db.Queryable<UserInfoEntity>()
                                .LeftJoin<ShopInfoEntity>((u, s) => u.Id == s.ShopManagerUserId)
                                .Where((u, s) => u.InvitationCode == invitationCode)
                                .Select((u, s) => new { UserInfo = u, ShopInfo = s })
                                .First();
                            if (us != null)
                            {
                                if (us.ShopInfo != null && us.ShopInfo.Id > 0)
                                {
                                    si = us.ShopInfo;
                                    break;
                                }

                                if (us.UserInfo != null)
                                {
                                    invitationCode = us.UserInfo.ReferenceInvitationCode;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return si;
        }

        public static IList<UserInfoAndShopInfoEntity> QueryByWhere(string name)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<ShopInfoEntity>()
                    .InnerJoin<UserInfoEntity>((s, u) => s.ShopManagerUserId == u.Id)
                    .Where((s, u) => s.Name.Contains(name))
                    .Select((s, u) => new UserInfoAndShopInfoEntity
                    {
                        ShopInfo = s,
                        UserInfo = u
                    })
                    .ToList();
                return ls;
            }
        }
    }
}
